package dk.liljedahl.christian.apx.shipdaemon;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.CBanner;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

public class ScriptEditor extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	
	private String scriptOriginal;

	private String testParser;
	private CLabel lblToSeeIf;
	private Composite composite_1;
	private Composite composite;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public ScriptEditor(Shell parent, int style, String scriptToEdit, String dialogName, String parser) {
		super(parent, style);
		setText(dialogName);
		scriptOriginal = scriptToEdit;
		testParser = parser; // we have "eventscript" and "responsescript"
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		
		text.setText(scriptOriginal);
		
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		result = scriptOriginal;
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(600, 474);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		text = new Text(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
		text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.stateMask == SWT.CTRL && e.keyCode == 97){     
					getTextScript().selectAll();
				}
			}
		});
		FormData fd_text = new FormData();
		fd_text.left = new FormAttachment(0, 10);
		fd_text.top = new FormAttachment(0, 10);
		fd_text.right = new FormAttachment(100, -10);
		text.setLayoutData(fd_text);
		
		composite = new Composite(shell, SWT.NONE);
		FormData fd_composite = new FormData();
		fd_composite.bottom = new FormAttachment(100, -10);
		fd_composite.left = new FormAttachment(0, 10);
		composite.setLayoutData(fd_composite);
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		Button btnTestThis = new Button(composite, SWT.NONE);
		btnTestThis.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				ExpressionParser expParser = new ExpressionParser();
				expParser.setAllParametersToDummyValues(); // We just need this cause its a parse-test
				// Set the script to the value of the textfield
				expParser.script = getTextScript().getText();
				// Find out wich parser to use
				if(testParser.equals("eventscript")){
					// Run the parser
					expParser.evaluateEventScript();
				}
				if(testParser.equals("responsescript")){
					// Run the parser
					expParser.evaluateResponseScript();
				}
				// Look at the error message. 
				if(expParser.error.equals("")){
					// Success
					// Write to the label
					getLblToSeeIf().setText("Script OK");
					// Set it to green
					getLblToSeeIf().setFont(SWTResourceManager.getFont("Tahoma", 8, SWT.BOLD));
					getLblToSeeIf().setForeground(SWTResourceManager.getColor(10, 150, 10));
				} else {
					// Failure
					// Write to the labe
					getLblToSeeIf().setText("Script Error");
					// Set it to red
					getLblToSeeIf().setFont(SWTResourceManager.getFont("Tahoma", 8, SWT.BOLD));
					getLblToSeeIf().setForeground(SWTResourceManager.getColor(220, 20, 60));
				}
			}
		});
		btnTestThis.setText("Test this");
		
		lblToSeeIf = new CLabel(composite, SWT.NONE);
		lblToSeeIf.setLayoutData(new RowData(240, SWT.DEFAULT));
		lblToSeeIf.setText("To see if it parses");
		
		composite_1 = new Composite(shell, SWT.NONE);
		fd_text.bottom = new FormAttachment(composite_1, -6);
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));
		FormData fd_composite_1 = new FormData();
		fd_composite_1.left = new FormAttachment(100, -174);
		fd_composite_1.bottom = new FormAttachment(100, -10);
		fd_composite_1.right = new FormAttachment(100, -10);
		composite_1.setLayoutData(fd_composite_1);
		
		Button btnOk = new Button(composite_1, SWT.NONE);
		btnOk.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// OK - We just close this window and the close event should return the content
				scriptOriginal = text.getText();
				shell.close();
			}
		});
		btnOk.setText("OK");
		
		Button btnCancel = new Button(composite_1, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// Then close the window
				shell.close();
			}
		});
		btnCancel.setText("Cancel");

	}
	
	protected CLabel getLblToSeeIf() {
		return lblToSeeIf;
	}
	protected Text getTextScript() {
		return text;
	}
}
